from PyQt5.QtCore import pyqtSignal
from bs4 import BeautifulSoup
import requests
from PyQt5 import QtWidgets, QtCore


class Gitee(QtCore.QThread):
    _signal = pyqtSignal(str)

    def __init__(self, key, lang):
        super(Gitee, self).__init__()
        self.page_no = 1
        self.results = []
        self.key = key
        self.lang = lang

    def __del__(self):
        self.wait()

    def run(self):
        self._signal.emit('开始爬取gitee')
        self.do_request()

    def do_request(self):
        url = f"https://search.gitee.com/?skin=rec&type=repository&q={self.key}&lang={self.lang}&pageno={self.page_no}"
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
        }
        # 发送请求并获取响应
        response = requests.get(url, headers=headers)
        soup = BeautifulSoup(response.content, 'html.parser')
        self.parse_content(soup)
        all_a = soup.findAll('a')
        for a in all_a:
            try:
                if f"?skin=rec&type=repository&q={self.key}&lang={self.lang}&pageno=" in a['href']:
                    if "下一页" in a.text and self.page_no <= 20:
                        self.page_no += 1
                        self.do_request()
                    else:
                        pass
            except Exception:
                pass

    def parse_content(self, soup):
        self._signal.emit(f"第{self.page_no}页数据如下:")
        resps = soup.findAll('a', class_="ns")
        for res in resps:
            item = str(res['href']).replace("?_from=gitee_search", "")
            if item not in 'https://gitee.com/openharmony':
                self.results.append(item)
                self._signal.emit(item)

    @property
    def signal(self):
        return self._signal
